(function(Jml,EFactory){
	
	EFactory.addControl("dropdown",{
		template : "<input type='text'  ${prop}  />",
		_initControl : function(target,op){
			var data = op.data;
			op.data = null;
			target.dropdown(op,data);
		}
	});
	
	var Control = $.IAdapter({
		mapping:{
			//渲染对象
			render:function(target,field,value){
				value != null && this.setValue(target,field,value);
				return target;
			},
			//根据字段获取对象值
			getValue:function(target,field){
				return field.renderer.getValue(target,field.format);
			},
			//根据字段设置对象值
			setValue:function(target,field,value){
				field.renderer.render(target,value,field.format);
			}
		},
		setDefault:true
	});
	
	//注册编辑类控件基类
	Control.add("edit",{
		//渲染方法
		render:function(container,field,value){
		    var ctr = field.editor.render(container,field.editorOp);
		    value != null && this.setValue(ctr,field,value);
		    container.after(container.children());
		    container.remove();
		    return ctr;
		},
		//获取值方法
		getValue:function(target,field)
		{
			return this.parseValue(field.editor.getValue(target),field);
		},
		//设置值方法
		setValue:function(target,field,value){
			field.editor.setValue(target,this.parseValue(value,field));
		},
		//数据转换方法
		parseValue:function(value,field){
			if(value != null)
			{
				return field.renderer.parse(value.value || value,field.format);
			}
			return value;
		}
	});
	
	EFactory.addControl('fieldControl', {
		template : '<span  ${prop} />',
		//构建配置信息，添加上下文渲染模式
//		_buildConfig : function() {
//			this.config.compile();
//			//this.getParent()._buildConfig();
//			this.config.option['renderType'] = this.page['renderType'] || this.config.option['renderType'];
//		},
		//执行控件初始化
		_initControl : function(target, op, view) {
			op = this.initField(this._initOp(op),target);
			ctr = this.render(op.render || this.parentView.renderType, target, op, view);
		},
		//初始化Field
		initField:function(field,target){
			//初始化渲染器
			if(!field.renderer || typeof(field.renderer) == "string")
				field.renderer = Ccay.UI.Renderer.get(field.renderer || field.type);
				
			field.renderer.initFormat && field.renderer.initFormat(field.format);
			
			(field.field) || (field.field = target.attr('name'));
			
			//初始化编辑器
			if(!field.editor || typeof(field.editor) == "string")
				field.editor = Ccay.Editor.get(field.editor || field.type);
			var propConf = this.config && this.config.prop;
			field.editorOp = $.extend(true,{
	            type: field.type,
	            field: field.field,
	            format: field.format,
	            prop : propConf,
	            id : propConf &&　propConf.id
	        },  this.transformOp(field.editorOp));

	        field.editor.setValid(field);
	        return field;
		},
		_initOp:function(op){
			op.type || (op.type = this.__name);
			return op;
		},
		//渲染字段
		render : function(type, container, op, view) {
			var ctr = Control.get(type).render(container, op, op.value);
			ctr.addClass('ccay-control ' + (this.cssRule || '')).attr('renderType', type);
			op.onRender && op.onRender(ctr, field, type);
			return ctr;
		},
		isNeedConf: false,
		transformOp:function(editorOp){
			if (this.isNeedConf) {
				var newOp = {};
				newOp.conf = editorOp;
				return newOp;
			}
			return editorOp;
		}
	});
	
	
	EFactory.push({
		text:{},
		number: {
			_initOp:function(op){
				op.type = op.dataType == "integer" ? "integer" : "number";
				this.cssRule = op.dataType == "integer" ? "integer" : "number";
				return op;
			}
		},
		"enum" : {
			isNeedConf:true
		},
		rate : {cssRule:"rate"},
		currency: {cssRule:"currency"},
		filesize : {},
		money : {},
		ldap: {
			isNeedConf:true
		},
		lookup: {},
		suggest: {
			isNeedConf:true
		},
		iCurrency: {},
		hidden: {},
		country: {},
		area: {},
		pbi: {},
		hrapprover: {},
		
//		date : {
//			cssRule : "date"
//		},
//		datetime : {
//			cssRule : "date"
//		},
//		dateym : {
//			cssRule : "date"
//		},
		bool : {},
		textarea : {template : '<div  ${prop} />'},
		
		popup : {
			isNeedConf:true
		},
        arrayEditor : {},
        
        popupProgramInfo : {},
        popupDimension: {},
        company : {},
        location : {},
        hrdept : {},
        personInfo : {},
        groupInfo : {}
	},"fieldControl");

	EFactory.add("date",{
		_initOp:function(op){
			if(op.dateMode){
				op.type = op.dateMode;
			}else if(!op.type){
				op.type = this.__name;
			}
			// 验证规则
			this.cssRule  = op.type;
			return op;
		}
	},"fieldControl");
	
//	/**
//	 * 
//	 */
//	EFactory.addControl("text",{
//		_initOp:function(op){
//			op.type = 'text';
//			return op;
//		}
//		
//	},"fieldControl");
//	/**
//	 * 
//	 */
//	EFactory.addControl("number",{
//		_initOp:function(op){
//			op.type = 'number';
//			return op;
//		}
//		
//	},"fieldControl");
//	/**
//	 * 
//	 */
//	EFactory.addControl("enum",{
//		_initOp:function(op){
//			op.type = 'enum';
//			return op;
//		}
//	
//	},"fieldControl");

	
	/**
	 * 日历
	 */
	EFactory.addControl("datepicker",{
		template : function(){
			//0占位符，为用户自定义的class，00228022
			var re = "<input type='text' class='ccay-calendar{0}'  ${prop} />",
			op = this.page.callFn(this.config.option),
			uClass = op.uClass || "";
			return re.formatValue(" "+uClass);
		},
		controlName : "calendar"
	});
	
	
	/**
	 * 日期组件
	 */
//	EFactory.addControl("date",{
//		
//		_initOp:function(op){
//			op.showTime = false;
//			return op;
//		}
//		
//	},"datepicker");
//	
//	/**
//	 * 日期+时间组件
//	 */
//	EFactory.addControl("datetime",{
//		
//		_initOp:function(op){
//			op.showTime = true;
//			return op;
//		}
//		
//	},'datepicker');
	
	/**
	 * 下拉框
	 */
	EFactory.addControl("combobox",{
		template : "<input type='text' class='init ccay-combobox' ${prop} />",
		_initControl : function(target,op){
			//combobox过后要整改成全部conf配置的，现在service有问题，还未整改，整改后就可以去掉 _initControl方法的重写
			if(op.service){
				//先将调用的方法保存到dataset
				op.dataset = Ccay.DataHelper.getObj(op.service.split(":")[1],this);
				target.attr("service",op.service);
				delete op.service;
			}
			target.ccayCombobox(op);
		}
	});
	
	/**
	 * 联想框
	 */
/*	EFactory.addControl("suggest",{
		controlName : "ccaySuggest",
		template : "<input type='text'  ${prop}  />"
	});*/
	
	/**
	 * 自定义联想框
	 */
/*	EFactory.addControl("suggestion",{
		controlName : "suggestion"
	},"suggest");*/
	
	/**
	 * 人员联想
	 */
/*	EFactory.addControl("ldap",{
		controlName : "ccayLdap",
		template : function(){
			var config = this.config;
			config.execute();
			var op = config.option, type = op.type || "text";
			if(type == "textarea"){
				return "<textarea class='init ccay-user'  ${prop}  />";
			}else{
				return "<input type='text' class='init ccay-user'  ${prop}  />";
			}
		}
	});*/
	
})(Ccay.Jml,Ccay.Jml.ElementFactory);